package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.config.OssConfig;
import com.example.demo.entity.*;
import com.example.demo.entity.DTO.GoodsDTO;
import com.example.demo.entity.DTO.GoodsNewDTO;
import com.example.demo.entity.DTO.WorkersAndBookedTimeDTO;
import com.example.demo.entity.VO.GoodsVO;
import com.example.demo.mapper.GoodsImgMapper;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.GoodsTypeMapper;
import com.example.demo.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Mr.Xiayh
 * @since 2024-06-04
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImgMapper goodsImgMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Autowired
    private OssConfig ossConfig;

    @Override
    public List<GoodsDTO> findAllByCriteria(Goods goods) {
        return getAllWorkersAndBeautyCare(goodsMapper.selectByCriteria(goods));
    }

    @Override
    public Goods getGoodsByProdId(Long prodId) {
        return goodsMapper.selectById(prodId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(value = "goods", key = "#prodId")
    })
    public void removeGoodsByProdId(Long prodId) {
        goodsMapper.deleteById(prodId);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "goods", key = "#prodId")
    })
    public void removeGoodsCacheByProdId(Long prodId) {

    }

    @Override
    public List<GoodsDTO> findAll() {
        return goodsMapper.findAllByCriteria();
    }

    @Override
    public List<GoodsNewDTO> queryList(Map<String, Object> param) {
        return goodsMapper.queryList(param);
    }

    @Override
    public boolean addGoods(Goods goods) {
        return goodsMapper.insertGoods(goods);
    }

    @Override
    public GoodsDTO queryObject(Integer id) {
        return goodsMapper.queryObject(id);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return goodsMapper.queryTotal(map);
    }

    private List<GoodsDTO> getAllWorkersAndBeautyCare(List<Goods> goodsList){
        List<GoodsDTO> goodsDTOS = new ArrayList<>();
        for (Goods goods : goodsList) {
            // 创建一个WorkersAndBookedTimeDTO实例，用于存储工人和预订时间的信息
            GoodsDTO goodsDTO = new GoodsDTO();

            // 将workersAndBookedTime对象的属性值复制到workersAndBookedTimeDTO对象中
            BeanUtil.copyProperties(goods, goodsDTO);

            // 查询商品图片 并存入DTO返回
            List<GoodsImg> selectGoodsImgs = goodsImgMapper.selectByGoodsId(goods.getGoodsId());
            List<GoodsImg> goodsImgs = new ArrayList<>();
            for (GoodsImg goodsImg : selectGoodsImgs) {
                goodsImg.setGoodsImg("https://" + ossConfig.getBucketName() + "." + ossConfig.getEndpoint()+ "/" +goodsImg.getGoodsImg());
                goodsImgs.add(goodsImg);
            }
            goodsDTO.setGoodsImgs(goodsImgs);

            // 查询商品类型 并存入DTO返回
            GoodsType goodsType = goodsTypeMapper.selectByTypeId(goods.getGoodsTypeId());
            goodsDTO.setGoodsType(goodsType);

            //放入List集合内
            goodsDTOS.add(goodsDTO);
        }
        return goodsDTOS;
    }
}
